package com.neouton.crm.screen.service.impl;

import com.neouton.crm.screen.Hander.MapResultHandler;
import com.neouton.crm.screen.entity.ScreenVO;
import com.neouton.crm.screen.mapper.ScreenMapper;
import com.neouton.crm.screen.service.IScreenService;
import com.neouton.feginClient.ReimburseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ScreenServiceImpl implements IScreenService {

    @Resource
    private ScreenMapper screenMapper;
    @Resource
    private ReimburseService reimburseService;

    @Override
    public Map<String, BigDecimal> queryAllByProjectId(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryAllByProjectId(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        //项目盈亏 项目总回款-总付款-开票税金+收票税金+退款总额
        map.put("项目盈亏",  map.get("已回款金额").subtract(map.get("采购付款金额")).subtract(map.get("开票税金"))
                .add(map.get("收票税金").add(map.get("退款总额"))).setScale(2, RoundingMode.HALF_DOWN));
        //未回款金额 合同金额-已回金额
        map.put("未回款金额", map.get("合同总金额").subtract(map.get("已回款金额")).setScale(2, RoundingMode.HALF_DOWN));
        //采购未付款金额 采购总金额-已付款金额
        map.put("未付款金额", map.get("采购总金额").subtract(map.get("采购付款金额")).setScale(2, RoundingMode.HALF_DOWN));
        return map;
    }

    /**
     * 查询各供应商采购数据统计
     * @param projectId
     * @return
     */
    @Override
    public Map<String, BigDecimal>  queryPurByProjectIdGroupSupper(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryPurByProjectIdGroupSupper(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        return map;
    }

    @Override
    public Map<String, BigDecimal>  queryPurDataByProjectId(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryPurDataByProjectId(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        //采购未付款金额 采购总金额-已付款金额
        map.put("未收票金额", map.get("采购付款金额").subtract(map.get("已收票金额")).setScale(2, RoundingMode.HALF_DOWN));
        return map;
    }

    /**
     * 查询各供应商采购数据统计
     * @param projectId
     * @return
     */
    public Map<String, BigDecimal>  queryPayData(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryPayData(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        map.put("未付款金额", map.get("采购总金额").subtract(map.get("采购付款金额")).setScale(2, RoundingMode.HALF_DOWN));
        return map;
    }

    /**
     * 查询各供应商采购数据统计
     * @param projectId
     * @return
     */
    public Map<String, BigDecimal>  queryNoPayData(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryNoPayData(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        return map;
    }

    /**
     * 查询各供应商采购数据统计
     * @param projectId
     * @return
     */
    public Map<String, BigDecimal>  queryReceiptRecordData(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryReceiptRecordData(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        return map;
    }

    /**
     * 查询各供应商采购数据统计
     * @param projectId
     * @return
     */
    public Map<String, BigDecimal>  queryReceiptRecordDataByUser(Integer projectId){
        MapResultHandler<String, BigDecimal> resultHandler = new MapResultHandler<>();
        screenMapper.queryReceiptRecordDataByUser(resultHandler,projectId);
        Map<String, BigDecimal> map = resultHandler.getMappedResults();
        return map;
    }




    @Override
    public List<ScreenVO> getContractInfo(Integer projectId) {
        ScreenVO contractInfo = screenMapper.getContractInfo(projectId);
        if (contractInfo == null){
            return new ArrayList<ScreenVO>();
        }
        contractInfo.setName("已录入金额");
        List<ScreenVO> contractList = new ArrayList<>();
        contractList.add(contractInfo);

        ScreenVO screenVO = new ScreenVO();
        screenVO.setName("未录入金额");
        BigDecimal projectSum = BigDecimal.valueOf(contractInfo.getProjectSum());
        BigDecimal value = BigDecimal.valueOf(contractInfo.getValue());

        screenVO.setValue(projectSum.subtract(value).doubleValue());
        screenVO.setProjectSum(contractInfo.getProjectSum());
        contractList.add(screenVO);
        return contractList;
    }

    @Override
    public List<ScreenVO> getContractInfoByCustomer(Integer projectId) {
        return screenMapper.getContractInfoByCustomer(projectId);
    }

    @Override
    public List<ScreenVO> getReimbursementInfo(Integer projectId) {
        return screenMapper.getReimbursementInfo(projectId);
    }

    @Override
    public List<ScreenVO> getRecordInfo(Integer projectId) {
        ScreenVO contractInfo1 = screenMapper.getContractInfo(projectId);
        if (contractInfo1 == null){
            return new ArrayList<ScreenVO>();
        }
        // 合同金额
        Double contractSum = contractInfo1.getValue();
        ScreenVO contractInfo = screenMapper.getRecordInfo(projectId);
        if (contractInfo == null){
            return new ArrayList<ScreenVO>();
        }
        contractInfo.setName("已回");
        List<ScreenVO> contractList = new ArrayList<>();
        contractList.add(contractInfo);

        ScreenVO screenVO = new ScreenVO();
        screenVO.setName("未回");
        BigDecimal contractSum1 = BigDecimal.valueOf(contractSum);
        BigDecimal bigDecimal = BigDecimal.valueOf(contractInfo.getValue());

        screenVO.setValue(contractSum1.subtract(bigDecimal).doubleValue());
        screenVO.setProjectSum(contractInfo.getProjectSum());
        contractList.add(screenVO);


        ScreenVO screenVO1 = new ScreenVO();
        screenVO1.setName("未录入");
        BigDecimal bigDecimal1 = BigDecimal.valueOf(contractInfo.getProjectSum());
        BigDecimal bigDecimal2 = BigDecimal.valueOf(contractSum);


        screenVO1.setValue(bigDecimal1.subtract(bigDecimal2).doubleValue());
        screenVO1.setProjectSum(contractInfo.getProjectSum());
        contractList.add(screenVO1);
        return contractList;
    }


    @Override
    public List<ScreenVO> getNotBackRecordInfo(Integer projectId) {
        ScreenVO contractInfo1 = screenMapper.getContractInfo(projectId);
        if (contractInfo1 == null){
            return new ArrayList<ScreenVO>();
        }
        // 合同金额
        Double contractSum = contractInfo1.getValue();
        ScreenVO contractInfo = screenMapper.getRecordInfo(projectId);
        if (contractInfo == null){
            return new ArrayList<ScreenVO>();
        }
        //未回款金额
        Double notBackMoney = contractSum - contractInfo.getValue();
        BigDecimal bgnotBackMoney = BigDecimal.valueOf(notBackMoney);
        //逾期金额
        Double overdueMoney = screenMapper.getOverdueMoney(projectId);
        BigDecimal bgoverdueMoney;
        if (overdueMoney == null){
            bgoverdueMoney = BigDecimal.valueOf(0);
        }
        bgoverdueMoney = BigDecimal.valueOf(overdueMoney);
        List<ScreenVO> contractList = new ArrayList<>();
        ScreenVO screenVO = new ScreenVO();
        screenVO.setName("未到期");
        screenVO.setValue(bgnotBackMoney.subtract(bgoverdueMoney).doubleValue());
        screenVO.setProjectSum(notBackMoney);
        contractList.add(screenVO);
        ScreenVO screenVO1 = new ScreenVO();
        screenVO1.setName("逾期");
        screenVO1.setValue(overdueMoney);
        screenVO1.setProjectSum(notBackMoney);
        contractList.add(screenVO1);
        return contractList;
    }
}
